<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>夏栀来信购物商城 - 购物车</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .cart-page {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .cart-container {
            display: flex;
            gap: 30px;
            margin-top: 20px;
        }
        .cart-items {
            flex: 2;
            overflow-x: auto;
        }
        .cart-items table {
            width: 100%;
            border-collapse: collapse;
        }
        .cart-items th, .cart-items td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        .cart-items th {
            background-color: #f9f9f9;
            font-weight: bold;
        }
        .cart-item-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .cart-item-info img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 4px;
        }
        .quantity-input {
            width: 60px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .remove-item {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .remove-item:hover {
            background-color: #c0392b;
        }
        .cart-summary {
            flex: 1;
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            height: fit-content;
        }
        .summary-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .summary-row.total {
            font-weight: bold;
            font-size: 18px;
            border-bottom: none;
        }
        .checkout-button {
            width: 100%;
            background-color: #27ae60;
            color: white;
            border: none;
            padding: 12px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 20px;
        }
        .checkout-button:hover {
            background-color: #2ecc71;
        }
        .empty-cart {
            text-align: center;
            padding: 50px 0;
            color: #666;
        }
    </style>
</head>
<body>
    <header>
        <div class="top-bar">
            <span>学号: 23215220329 姓名: 牟其鑫</span>
            <div class="user-actions">
                <a href="login.html">登录</a> | <a href="register.html">注册</a>
                | <a href="checkout.html" class="user-center">个人中心</a>
            </div>
        </div>
        <div class="main-header">
            <div class="logo">夏栀来信</div>
            <div class="search-box">
                <form id="search-form" action="product.html" method="get">
                    <input type="text" name="name" placeholder="搜索商品名称..." required>
                    <button type="submit">搜索</button>
                </form>
            </div>
            <div class="cart-icon">
                <a href="cart.html">购物车(<span id="cart-count">0</span>)</a>
            </div>
        </div>
        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="#">热门推荐</a></li>
                <li><a href="#">节日限定</a></li>
                <li><a href="#">园艺工具</a></li>
                <li><a href="#">养护指南</a></li>
                <li><a href="#">常见问题</a></li>
            </ul>
        </nav>
    </header>

    <main class="cart-page">
        <h1>我的购物车</h1>
        <div class="cart-container">
            <div class="cart-items">
                <table>
                    <thead>
                        <tr>
                            <th>商品信息</th>
                            <th>单价</th>
                            <th>数量</th>
                            <th>小计</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="cart-items-list">
                        <!-- 购物车商品将通过JS动态加载 -->
                    </tbody>
                </table>
            </div>
            <div class="cart-summary">
                <h3>订单摘要</h3>
                <div class="summary-row">
                    <span>商品总价</span>
                    <span id="subtotal">¥0.00</span>
                </div>
                <div class="summary-row">
                    <span>运费</span>
                    <span id="shipping">¥0.00</span>
                </div>
                <div class="summary-row total">
                    <span>应付总额</span>
                    <span id="total">¥0.00</span>
                </div>
                <button id="checkout-btn" class="checkout-button">提交订单</button>
            </div>
        </div>
    </main>

    <footer>
        <div class="footer-content">
            <p>© 2023 夏栀来信网上购物商城 版权所有</p>
            <p>地址：某某省某某市某某区某某路123号</p>
            <p>联系电话：123-45678901</p>
        </div>
    </footer>

    <script>
        // 购物车功能
        document.addEventListener('DOMContentLoaded', function() {
            const cartItemsList = document.getElementById('cart-items-list');
            const subtotalElement = document.getElementById('subtotal');
            const shippingElement = document.getElementById('shipping');
            const totalElement = document.getElementById('total');
            
            // 从本地存储加载购物车
            let cart = JSON.parse(localStorage.getItem('cart')) || [];
            
            // 更新购物车数量显示（供其他页面调用）
            window.updateCartCount = function() {
                const count = cart.reduce((total, item) => total + item.quantity, 0);
                document.getElementById('cart-count').textContent = count;
            };
            
            // 渲染购物车商品
            function renderCart() {
                cartItemsList.innerHTML = '';
                
                if (cart.length === 0) {
                    cartItemsList.innerHTML = `
                        <tr>
                            <td colspan="5">
                                <div class="empty-cart">
                                    <p>您的购物车是空的</p>
                                    <a href="index.html" class="continue-shopping">继续购物</a>
                                </div>
                            </td>
                        </tr>
                    `;
                    subtotalElement.textContent = '¥0.00';
                    shippingElement.textContent = '¥0.00';
                    totalElement.textContent = '¥0.00';
                    updateCartCount();
                    return;
                }
                
                let subtotal = 0;
                
                cart.forEach(item => {
                    const itemTotal = item.price * item.quantity;
                    subtotal += itemTotal;
                    
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>
                            <div class="cart-item-info">
                                <img src="${item.image}" alt="${item.name}">
                                <span>${item.name}</span>
                            </div>
                        </td>
                        <td>¥${item.price.toFixed(2)}</td>
                        <td>
                            <input type="number" min="1" value="${item.quantity}" 
                                   data-id="${item.id}" class="quantity-input">
                        </td>
                        <td>¥${itemTotal.toFixed(2)}</td>
                        <td><button class="remove-item" data-id="${item.id}">删除</button></td>
                    `;
                    cartItemsList.appendChild(row);
                });
                
                // 计算运费和总额
                const shipping = subtotal > 100 ? 0 : 10;
                const total = subtotal + shipping;
                
                subtotalElement.textContent = `¥${subtotal.toFixed(2)}`;
                shippingElement.textContent = `¥${shipping.toFixed(2)}`;
                totalElement.textContent = `¥${total.toFixed(2)}`;
                
                // 添加事件监听器
                document.querySelectorAll('.quantity-input').forEach(input => {
                    input.addEventListener('change', function() {
                        const productId = this.getAttribute('data-id');
                        const newQuantity = parseInt(this.value);
                        
                        if (newQuantity > 0) {
                            const item = cart.find(item => item.id === productId);
                            if (item) {
                                item.quantity = newQuantity;
                                localStorage.setItem('cart', JSON.stringify(cart));
                                renderCart();
                            }
                        } else {
                            this.value = 1;
                        }
                    });
                });
                
                document.querySelectorAll('.remove-item').forEach(button => {
                    button.addEventListener('click', function() {
                        const productId = this.getAttribute('data-id');
                        cart = cart.filter(item => item.id !== productId);
                        localStorage.setItem('cart', JSON.stringify(cart));
                        renderCart();
                    });
                });
                
                updateCartCount();
            }
            
            // 初始渲染
            renderCart();
            
            // 结算按钮事件
            document.getElementById('checkout-btn').addEventListener('click', function() {
                if (cart.length === 0) {
                    alert('购物车为空，无法提交订单');
                    return;
                }
                window.location.href = 'checkout.html';
            });
        });
    </script>
</body>
</html>